package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "LoginFilter",value = "/vip/*")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest) req;

        HttpSession session = request.getSession();

        String isLogin = (String) session.getAttribute("isLogin");

        if(isLogin!=null){
            chain.doFilter(req, resp);
        }else{

            resp.getWriter().print("<script>alert('请先登录');window.location.href='../login.jsp'</script>");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
